<template>
  <div>
    <tableComponents
      :isHaveExport="false"
      :isShow="false"
      :isHavePagination="false"
      @toAdd="editHotelHome"
    >
      <template v-slot:table>
        <el-table
          ref="multipleTable"
          :data="list.data"
          stripe
          v-loading="loading"
          header-row-class-name="head_color"
          tooltip-effect="dark"
          style="width: 99.8%"
        >
          <el-table-column label="LOGO" min-width="100">
            <template slot-scope="scope">
              <el-popover placement="right" title trigger="hover">
                <img :src="$fnc.getImgUrl(scope.row.piclink)" style="width:300px;" />
                <img
                  slot="reference"
                  :src="scope.row.piclink"
                  :alt="scope.row.img"
                  style="width:100px"
                />
              </el-popover>
            </template>
          </el-table-column>

          <el-table-column label="标题" min-width="120">
            <template slot-scope="scope">{{ scope.row.title }}</template>
          </el-table-column>

          <el-table-column label="操作" min-width="120">
            <template slot-scope="scope">
              <el-row class="myblank">
                <el-button
                  :loading="$store.state.isLoading"
                  size="mini"
                  @click="editHotelHome(scope.row)"
                  type="primary"
                >编辑</el-button>
                <el-button
                  :loading="$store.state.isLoading"
                  size="mini"
                  type="danger"
                  @click="deleteNew(scope.row.id)"
                >删除</el-button>
              </el-row>
            </template>
          </el-table-column>
        </el-table>
      </template>
    </tableComponents>

    <el-dialog
      :close-on-click-modal="false"
      :title="HotelHome.id ? '编辑' : '添加'"
      :modal-append-to-body="false"
      :visible.sync="showHotelHome"
      class="tl size_13"
      width="800px"
    >
      <el-form :model="HotelHome" label-width="120px">
        <el-form-item label="标题" required>
          <el-col :span="12">
            <el-input v-model="HotelHome.title" clearable></el-input>
          </el-col>
        </el-form-item>

        <el-form-item label="图片地址" required>
          <el-col :span="12" class="tl">
            <el-input v-model="HotelHome.piclink" disabled autocomplete="off" clearable class="mar_bottom_5"></el-input>
            <uploadpic
              :piclink="HotelHome.piclink"
              :width="150"
              :height="150"
              :is_more="false"
              @send_pic="onSuccess1"
            ></uploadpic>
          </el-col>
        </el-form-item>
        
        <el-form-item>
          <el-button
            :loading="$store.state.isLoading"
            size="medium"
            type="primary"
            @click.prevent="subHotelHome"
          >确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import uploadpic from "@/components/page/pic/up_pic";
export default {
  data() {
    return {
      loading: false,
      list: [],
      HotelHome: {},
      showHotelHome: false,
    };
  },
  components: {
    uploadpic
  },
  methods: {
    onSuccess1(pic) {
      this.HotelHome.piclink = pic;
    },
    editHotelHome(item) {
      if (item) {
        this.HotelHome = item;
      } else {
        this.HotelHome = {
          piclink: ""
        };
      }
      this.showHotelHome = true;
    },
    subHotelHome() {
      if (this.HotelHome.id) {
        this.$api.getHotel
          .hotel_label_logo_saveedit(this.HotelHome)
          .then(res => {
            if (res.data.code == 200) {
              this.$fnc.alertSuccess("操作成功");
              this.showHotelHome = false;
              this.getNewsAll();
            } else {
              this.$fnc.alertError(res.data.result);
            }
          });
      } else {
        this.$api.getHotel
          .hotel_label_logo_saveadd(this.HotelHome)
          .then(res => {
            if (res.data.code == 200) {
              this.$fnc.alertSuccess("操作成功");
              this.showHotelHome = false;
              this.getNewsAll();
            } else {
              this.$fnc.alertError(res.data.result);
            }
          });
      }
    },
    getNewsAll() {
      this.loading = true;
      this.$api.getHotel.hotel_label_logo_lists({}).then(res => {
        if (res.data.code == 200) {
          this.list = res.data.result;
        }
        this.loading = false;
      });
    },
    deleteNew(id) {
      this.$swal({
        title: "您确定要删除这条信息吗",
        text: "删除后将无法恢复，请谨慎操作！",
        type: "warning",
        showCancelButton: true,
        confirmButtonColor: "#DD6B55",
        confirmButtonText: "是的，我要删除",
        cancelButtonText: "容我三思"
      }).then(willDelete => {
        if (willDelete.value) {
          var data = { id: id };
          this.$api.getHotel.hotel_label_logo_del(data).then(res => {
            if (res.data.code == 200) {
              this.$swal("删除成功！", "您已经删除了这条信息。", "success");
              this.getNewsAll();
            } else {
              this.$swal("删除失败！", res.data.result, "error");
            }
          });
        }
      });
    }
  },
  created() {
    this.getNewsAll();
  }
};
</script>

<style lang="less" scoped>
.myblank button {
  margin: 5px 5px 0 0;
}
.user_table span {
  font-size: 13px;
}
</style>
